<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="demoTable">
            资产名
            <div class="layui-inline">
                <input class="layui-input" name="name" id="name" autocomplete="off">
            </div>
            类型
            <div class="layui-inline">
                <input class="layui-input" name="type" id="type" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload">根据资产名或类型搜索</button>
        </div>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">

        </table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">更新</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script th:src="@{lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>

<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: 'asset/assetAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 100, title: 'ID', sort: true},
                    {field: 'name', width: 100, title: '资产名'},
                    {field: 'type', width: 100, title: '类型'},
                    {field: 'model', width: 100, title: '型号'},
                    {field: 'price', width: 100, title: '价格', sort: true},
                    {field: 'createTime', width: 200, title: '入库时间', sort: true},
                    {field: 'updateTime', width: 200, title: '更新时间'},
                    {field: 'remarks', width: 200, align: 'center', title: '备注'},
                    {
                        field: 'status', title: '状态', width: 100,
                        templet: function (res) {
                            if (res.status == 1) {
                                return '<span class="layui-badge layui-bg-blue">库外</span>';
                            } else if (res.status == 2) {
                                return '<span class="layui-badge-rim">正常</span>';
                            } else if (res.status == 3) {
                                return '<span class="layui-badge layui-bg-red">异常</span>';
                            }
                        }
                    },
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            id: 'testReload'
        });

        var $ = layui.$, active = {
            reload: function () {
                //var demoReload = $('#demoReload');
                var nameId = $('#name');
                var type = $('#type');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        name: nameId.val().trim(),//trim去空格的
                        type: type.val().trimEnd()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加资产',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['70%', '70%'],
                    content: 'addAsset',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                // var checkStatus = table.checkStatus('currentTableId')
                //     , data = checkStatus.data;
                // layer.alert(JSON.stringify(data));

                //选中信息
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                console.log(data);
                if (data.length == 0) {
                    layer.msg("请选择要删除的记录信息");
                } else {//删除工作
                    //获取ids
                    var ids = getCheckId(data);
                    console.log(ids);
                    layer.confirm('你真的要删除吗？', function (index) {
                        deleteUserByIds(ids, index);
                    })
                }
            }
        });

        //获取选中id的集合
        function getCheckId(data) {
            var arr = new Array();
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].id);
            }
            return arr.join(",");
        }


        /**
         *根据id删除记录信息
         */
        function deleteUserByIds(ids, index) {
            $.ajax({
                url: "asset/deleteAssetByIds",
                type: "POST",
                data: {'ids': ids},
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg("删除成功", {
                            icon: 6,
                            time: 500,
                        }, function () {
                            parent.window.location.reload();//重新页面
                            var ifremeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(ifremeIndex);
                        })
                    } else {
                        layer.msg("删除失败");
                    }
                }
            })
            return false;
        };

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑资产',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['60%', '60%'],
                    content: 'updateAsset?id=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //调用删除函数实现删除工作
                    deleteUserByIds(data.id, index);
                });
            }
        });

    });
</script>

</body>
</html>